package com.group4.DB;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.group4.DAO.UserDAO;
import com.group4.Utility.Mail;
import com.mysql.jdbc.UpdatableResultSet;

public class User {

	private int ID;
	private String name;
	private String surname;
	private String username;
	private String password;
	private String email;

	static PreparedStatement ps = null;
	static ResultSet rs = null;
	
	public static UserDAO getUserWithID(int ID) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		UserDAO user = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_User where ID = ?");

			ps.setInt(1, ID);

			rs = ps.executeQuery();

			while (rs.next()) {

				user = new UserDAO();

				user.setID(ID);
				user.setName(rs.getString("name"));
				user.setSurname(rs.getString("surname"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				user.setEnabledUser(rs.getInt("enabledUser"));
				user.setActivationKey(rs.getString("activationKey"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getUserWithID. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getUserWithID. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithID. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithID. ");
					e.printStackTrace();
				}
			}
		}
		return user;
	}
	public static UserDAO getUserWithUsername(String username) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		UserDAO user = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_User where username = ?");

			ps.setString(1, username);

			rs = ps.executeQuery();

			while (rs.next()) {

				user = new UserDAO();

				user.setID(rs.getInt("ID"));
				user.setName(rs.getString("name"));
				user.setSurname(rs.getString("surname"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				user.setEnabledUser(rs.getInt("enabledUser"));
				user.setActivationKey(rs.getString("activationKey"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getUserWithUsername. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getUserWithUsername. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithUsername. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithUsername. ");
					e.printStackTrace();
				}
			}
		}
		return user;
	}
	
	public static UserDAO getUserWithEmail(String email) {

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return null;
		}

		UserDAO user = null;

		try {
			ps = ConnectionManager.conn.prepareStatement("select * from tbl_User where email = ?");

			ps.setString(1, email);

			rs = ps.executeQuery();

			while (rs.next()) {

				user = new UserDAO();

				user.setID(rs.getInt("ID"));
				user.setName(rs.getString("name"));
				user.setSurname(rs.getString("surname"));
				user.setUsername(rs.getString("username"));
				user.setPassword(rs.getString("password"));
				user.setEmail(rs.getString("email"));
				user.setEnabledUser(rs.getInt("enabledUser"));
				user.setActivationKey(rs.getString("activationKey"));
			}

		} catch (SQLException e) {
			System.out.println("SQL Error in getUserWithEmail. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in getUserWithEmail. ");
			e.printStackTrace();
		} finally {

			if (rs != null) {
				try {
					rs.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithEmail. ");
					e.printStackTrace();
				}
			}
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("Error in getUserWithEmail. ");
					e.printStackTrace();
				}
			}
		}
		return user;
	}


	public static boolean addUser(UserDAO user) {
		
		String validationUrl = "http://titan.cmpe.boun.edu.tr:8083/Signport/validate?username=";
		validationUrl += user.getUsername();
		validationUrl += "&activationKey=";
		validationUrl += user.getActivationKey();
		
		Mail mail = new Mail(user.getEmail(), "Welcome to SignPort", validationUrl);
		mail.send();
		
		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			ps = ConnectionManager.conn
					.prepareStatement(" insert into tbl_User (name, surname, username, password, email, activationKey)"
							+ " values (?, ?, ?, ?, ?, ?)");

			ps.setString(1, user.getName());
			ps.setString(2, user.getSurname());
			ps.setString(3, user.getUsername());
			ps.setString(4, user.getPassword());
			ps.setString(5, user.getEmail());
			ps.setString(6, user.getActivationKey());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in addUser. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in addUser. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addUser. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean updateUser(UserDAO user) {

		boolean result = false;

		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		try {
			UserDAO oldUser = getUserWithID(user.getID());

			if (oldUser == null) {
				System.out.println("No User found with ID = " + user.getID());
				return result;
			}

			ps = ConnectionManager.conn
					.prepareStatement("update tbl_User set name = ?, surname = ?, username = ?, password = ?, email = ?, enabledUser = ? where (ID = ?)");

			ps.setString(1, user.getName());
			ps.setString(2, user.getSurname());
			ps.setString(3, user.getUsername());
			ps.setString(4, user.getPassword());
			ps.setString(5, user.getEmail());
			ps.setInt(6, user.getEnabledUser());
			ps.setInt(7, user.getID());

			ps.executeUpdate();

			result = true;
		} catch (SQLException e) {
			System.out.println("SQL Error in updateUser. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in updateUser. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in addUser. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}

	public static boolean deleteUser(int ID) {
		boolean result = false;
		
		if (ConnectionManager.conn == null) {
			System.out.println("Connection is null.");
			return result;
		}

		PreparedStatement ps = null;
		UserDAO User = null;

		try {
			UserDAO oldUser = getUserWithID(ID);

			if (oldUser == null) {
				System.out.println("No User found with ID = " + User.getID());
				return result;
			}
			
			ps = ConnectionManager.conn.prepareStatement("delete from tbl_User where ID = ?");

			ps.setInt(1, ID);

			ps.execute();
			
			result = true;

		} catch (SQLException e) {
			System.out.println("SQL Error in deleteUser. ");
			e.printStackTrace();
		} catch (Exception e) {
			System.out.println("Exception in deleteUser. ");
			e.printStackTrace();
		} finally {
			if (ps != null) {
				try {
					ps.close();
				} catch (SQLException e) {
					System.out.println("SQL Error in deleteUser. ");
					e.printStackTrace();
				}
			}
		}
		return result;
	}
	public static boolean userExist(String username){
		boolean exist = false;
		String query = "SElECT * FROM tbl_User WHERE (username=?)";
		try {
			ps = ConnectionManager.conn.prepareStatement(query);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if(rs.next()){
				exist = true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			System.out.println("Error in checking whether a user exist");
			e.printStackTrace();
		}
		return exist;
	}
	
	public static void activateUser(String username, String activationKey){
		UserDAO user = User.getUserWithUsername(username);
		System.out.println(user.getActivationKey());
		System.out.println(activationKey);
		if(user != null && user.getActivationKey().equals(activationKey)){
			user.setEnabledUser(1);
			User.updateUser(user);
		}
	}
	
	public static UserDAO getUser(String email, String activationKey){
		UserDAO user = User.getUserWithEmail(email);
		if(user != null && user.getActivationKey().equals(activationKey)){
			return user;
		}
		return null;
	}
	
	public static boolean validate(String username, String password) {
		// TODO Auto-generated method stub
		String query = "SElECT * FROM tbl_User WHERE (username=?)";
		String password2 = "";
		try {
			ps = ConnectionManager.conn.prepareStatement(query);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if(rs.next()){
				password2 = rs.getString(5);
			}
			System.out.println(password);
		}catch(SQLException e){
			e.printStackTrace();
		}
		if(password.equals(password2)){
			return true;
		}
		return false;
	}
	
	public static UserDAO validateUser(String username, String password) {
		// TODO Auto-generated method stub
		String query = "SElECT * FROM tbl_User WHERE (username=?)";
		String password2 = "";
		try {
			ps = ConnectionManager.conn.prepareStatement(query);
			ps.setString(1, username);
			rs = ps.executeQuery();
			if(rs.next()){
				password2 = rs.getString(5);
			}
			if(password.equals(password2)){
				UserDAO user = new UserDAO();
				
				user.setID(rs.getInt(1));
				user.setName(rs.getString(2));
				user.setSurname(rs.getString(3));
				user.setUsername(rs.getString(4));
				user.setPassword(rs.getString(5));
				user.setEmail(rs.getString(6));
				user.setEnabledUser(rs.getInt(7));
				return user;
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
		
		return null;
	}
}
